/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sn.controller;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sn.metier.Partage;
import sn.metier.Personne;
import sn.service.NotificationService;
import sn.service.PartageService;
import sn.service.PersonneService;

/**
 *
 * @author Pluce
 */
@Controller
public class AmisController {
    
    @Autowired
    private PersonneService personneService;
    @Autowired
    private PartageService partageService;
    
    @Autowired
    private NotificationService notificationService;
    
    @Transactional
    @RequestMapping(value="/amis",method=RequestMethod.GET)
    public String afficherAmis(Model model,HttpSession session){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Personne user = personneService.voirPersonne((Long)session.getAttribute("user"));
        List<Personne> amis = user.getAmis();
        amis.size();
        model.addAttribute("lesAmis",amis);
        model.addAttribute("user",user);
        return "amis";
    }
    
    @Transactional
    @RequestMapping(value="/amis",method=RequestMethod.POST)
    public String demanderAmis(Model model,HttpSession session,Long destinataire, String message){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long auteur = (Long)session.getAttribute("user");
        
        notificationService.demanderAmitie(auteur, destinataire, message);

        model.addAttribute("success","Demande d'amitié bien envoyée.");
        Personne user = personneService.voirPersonne(auteur);
        model.addAttribute("user",user);
        
        List<Personne> amis = user.getAmis();
        amis.size();
        model.addAttribute("lesAmis",amis);
        
        return "amis";
    }

    @Transactional
    @RequestMapping(value="/marquer_lu",method=RequestMethod.GET)
    public String lireNoteIdentification(Model model,HttpSession session,@RequestParam Long id){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long auteur = (Long)session.getAttribute("user");

        notificationService.luIdentification(auteur, id);
        return "redirect:index.htm";
    }
    
    @Transactional
    @RequestMapping(value="/accepter_ami",method=RequestMethod.GET)
    public String accepterAmi(Model model,HttpSession session,@RequestParam Long id){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long auteur = (Long)session.getAttribute("user");
        
        Personne nouvel = personneService.accepterAmi(auteur,id);        
        
        return "redirect:profil.htm?id="+nouvel.getId();
    }
    
    @Transactional
    @RequestMapping(value="/refuser_ami",method=RequestMethod.GET)
    public String refuserAmi(Model model,HttpSession session,@RequestParam Long id){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long auteur = (Long)session.getAttribute("user");
        
        personneService.refuserAmi(auteur,id);        
        
        return "redirect:index.htm";
    }
    
    @Transactional
    @RequestMapping(value="/rechercher",method=RequestMethod.POST)
    public String recherche(Model model, HttpSession session, String type, String recherche){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long auteur = (Long)session.getAttribute("user");
        List<Personne> result;
        if(type.equals("nom")){
            result = personneService.chercherAmiParNom(auteur,recherche);
        } else if(type.equals("prenom")){
            result = personneService.chercherAmiParPrenom(auteur,recherche);
        } else {
            result = personneService.chercherAmiParPseudo(auteur,recherche);
        }
        model.addAttribute("lesAmis",result);
        model.addAttribute("recherche",type);
        
        Personne user = personneService.voirPersonne((Long)session.getAttribute("user"));
        model.addAttribute("user",user);
        return "amis";
    }
    
 
    @Transactional
    @RequestMapping(value="/profil",method=RequestMethod.GET)
    public String profil(Model model, HttpSession session,@RequestParam Long id){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long idVisiteur = (Long)session.getAttribute("user");
        Personne visiteur = personneService.voirPersonne(idVisiteur);
        Personne profil = personneService.voirPersonne(id);
        model.addAttribute("user", visiteur);
        List<Personne> amis = visiteur.getAmis();
        amis.isEmpty();
        model.addAttribute("amis",amis);
        if(profil == null){
            return "ami404";
        }
        List<Partage> mur = partageService.voirMur(idVisiteur, id);
        model.addAttribute("murAmi",mur);
        model.addAttribute("profilAmi", profil);
        return "profil";
    }
    
    @RequestMapping(value="/supprimer_ami",method=RequestMethod.GET)
    public String supprimerAmi(HttpSession session, Long id){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long idVisiteur = (Long)session.getAttribute("user");
        personneService.supprimerAmi(idVisiteur, id);
        return "redirect:/amis.htm";
    }
}
